<?php
// +----------------------------------------------------------------------
// | 文件: index.php
// +----------------------------------------------------------------------
// | 功能: 提供todo api接口
// +----------------------------------------------------------------------
// | 时间: 2021-11-15 16:20
// +----------------------------------------------------------------------
// | 作者: rangangwei<gangweiran@tencent.com>
// +----------------------------------------------------------------------

namespace app\controller;

use Error;
use Exception;
use app\model\Counters;
use think\response\Html;
use think\response\Json;
use think\facade\Log;

class Index2
{

    public function sendRequest($data,$url){
        $headerArray = array(
            "Content-Type: application/xml",
            "Accept: application/xml",
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);

        $real1 = realpath('/app/public/apiclient_cert.pem');
        $real2 = realpath('/app/public/apiclient_key.pem');
        curl_setopt($curl, CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($curl, CURLOPT_SSLCERT,$real1);
        curl_setopt($curl, CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($curl, CURLOPT_SSLKEY,$real2);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        libxml_disable_entity_loader(true);//curl返回的数据.在页面中展示是不会显示xml标签的.会将标签变成实体字符.这一行代表的是禁止变成实体字符
        $xml = \simplexml_load_string($output,'SimpleXMLElement', LIBXML_NOCDATA); //创建 SimpleXML对象
        $val = json_decode(json_encode($xml),true);//这样就变成数组了
        curl_close($curl);
        return $val;
    }

    public function sendHongBao($to_openid = 'omnJQwph51NkUJNoFHq08zUzP5Dk'):Json
    {
        $act_name = '司米新春盲盒';
        $client_ip = '81.68.186.53';
        $mch_id = '1448601902';
        $mch_billno = $mch_id.date('Ymd').mt_rand(1000000000,9999999999);
        $nonce_str = md5('mosaic2022');
        $re_openid = $to_openid;
        $remark = '整体厨房,定制家具';
        $scene_id = 'PRODUCT_2';
        $send_name = '司米定制家居';
        $total_amount = 30;
        $total_num = 1;
        $wxappid = 'wxcc7d452635a77e1d';
        $wishing = '感谢您参加';
        $key = 'qpwoeirutyalskdjfhgzmxn123456789';
        $sign = $this->signEncry($act_name,$client_ip,$mch_billno,$mch_id,$nonce_str,$re_openid,$remark,$scene_id,$send_name,$total_amount,$total_num,$wishing,$wxappid,$key);
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $xml =
            '<xml>' .
            '<act_name>' . $act_name . '</act_name>' .
            '<client_ip>' . $client_ip . '</client_ip>' .
            '<mch_billno>' . $mch_billno . '</mch_billno>' .
            '<mch_id>' . $mch_id . '</mch_id>' .
            '<nonce_str>' . $nonce_str . '</nonce_str>' .
            '<re_openid>' . $re_openid . '</re_openid>' .
            '<remark>' . $remark . '</remark>' .
            '<scene_id>' . $scene_id . '</scene_id>' .
            '<send_name>' . $send_name . '</send_name>' .
            '<total_amount>' . $total_amount . '</total_amount>' .
            '<total_num>' . $total_num . '</total_num>' .
            '<wishing>' . $wishing . '</wishing>' .
            '<wxappid>' . $wxappid . '</wxappid>' .
            '<sign>' . $sign . '</sign>' .
            '</xml>';
        try {
            $res = $this->sendRequest($xml,$url);
            $counters = new Counters;
            $counters->create(
                ["unionId" => json_encode($res)],
                ["unionId"],
                true
            );
        }catch (Exception $e){
            $counters = new Counters;
            $counters->create(
                ["unionId" => $e->getMessage()],
                ["unionId"],
                true
            );
        }

        return json(['success']);
    }

    private function signEncry($act_name,$client_ip,$mch_billno,$mch_id,$nonce_str,$re_openid,$remark,$scene_id,$send_name,$total_amount,$total_num,$wishing,$wxappid,$key)
    {
        $stringSignTemp =
            'act_name=' .$act_name .
            '&client_ip=' .$client_ip .
            '&mch_billno=' .$mch_billno .
            '&mch_id=' .$mch_id .
            '&nonce_str=' .$nonce_str .
            '&re_openid=' .$re_openid .
            '&remark=' .$remark .
            '&scene_id=' .$scene_id .
            '&send_name=' .$send_name .
            '&total_amount=' .$total_amount .
            '&total_num=' .$total_num .
            '&wishing=' .$wishing .
            '&wxappid=' .$wxappid .
            '&key=' .$key;
        return strtoupper(md5($stringSignTemp));
    }

}
